//
//  AddStoreVc.m
//  WGFGlobalHouses
//
//  Created by welldone on 15/5/22.
//  Copyright (c) 2015年 waiguofang. All rights reserved.
//

#import "AddStoreVc.h"
#import "BdTV.h"
#import "config.h"
#import "LocPickView.h"
#import "BrokerDM.h"

#import "MBProgressHUD+MJ.h"
#import "StringTool.h"
#import "UIView+circular.h"


@interface AddStoreVc ()<UITextFieldDelegate>
@property(nonatomic,strong) LocPickView *lockPickView;
@property(nonatomic,strong) BrokerDM *dm;
@property (weak, nonatomic) IBOutlet UITextField *storeNametf;
@property (weak, nonatomic) IBOutlet UIButton *saveBtn;

@end

@implementation AddStoreVc
@synthesize lockPickView,dm;


- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initView];
}


-(void)initView{
    
    [self.saveBtn viewToCircular];
    BdTV *title = [[BdTV alloc]initWithFrame:CGRectMake(0, 0, PMW, 44)];
    [title.backBtn setTag:11];
    [title.backBtn addTarget:self action:@selector(btnOnclick:) forControlEvents:UIControlEventTouchUpInside];
    [[title doneBtn] removeFromSuperview];
    
    if (self.lastvcType == isOtherVC) {
        [title setMianTitle:@"添加门店"];
    }else if (self.lastvcType == isStoreViewController)
    {
        [title setMianTitle:@"修改信息"];
        self.storeNametf.text = self.model.storeName;
    }
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:title];
    
    lockPickView = [LocPickView creadInstance];
    dm = [[BrokerDM alloc]init];
}





-(void)btnOnclick:(UIButton*)sender{
    
    if(sender.tag==11){
        [self.navigationController popViewControllerAnimated:YES];
    }
    
}


- (IBAction)locationBtnOnClick:(id)sender {
    if (lockPickView != nil) {
        [lockPickView showInView:self.view];
    }
    
}

#pragma mark 点击保存
- (IBAction)saveBtnOnClick:(id)sender {
    
    if ([StringTool isBlankString:self.storeNametf.text])
    {
        [MBProgressHUD showError:@"请输入门店名称"];
        return;
    }
    if (self.lastvcType == isOtherVC)
    {
        [self addStoreupdata];
        return;
    }
    [self modifystore2];
    
}

#pragma mark  修改上传数据
-(void)modifyStore
{
    [MBProgressHUD showMessage:@"请稍等" toView:self.view];
    
     __weak typeof(self) baseSelf = self;
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        
        
        ResulteMod *mod = [dm createshopWith:baseSelf.storeNametf.text];
        dispatch_async(dispatch_get_main_queue(), ^{
            
            [MBProgressHUD hideHUDForView:baseSelf.view animated:YES];
            
            [baseSelf.navigationController popViewControllerAnimated:YES];
            
            if (mod.resltState == ResultFail) {
                [MBProgressHUD showError:mod.resultTip];
            }else if (mod.resltState == ResultNetError)
            {
                [MBProgressHUD showError:@"网络错误"];
            }
            else
            {
                [MBProgressHUD showSuccess:@"添加成功"];
            }
            
        });
    });
    
}


#pragma mark linda xiugaimendian
-(void)modifystore2
{
    [MBProgressHUD showMessage:@"请稍等" toView:self.view];
    
    __weak typeof(self) baseSelf = self;
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        
        ResulteMod *mod = [dm modifystoreWithStoreId:baseSelf.model.storeId withStoreName:baseSelf.storeNametf.text];
        
        dispatch_async(dispatch_get_main_queue(), ^{
            
            [MBProgressHUD hideHUDForView:baseSelf.view animated:YES];
            
            [baseSelf.navigationController popViewControllerAnimated:YES];
            
            if (mod.resltState == ResultFail) {
                [MBProgressHUD showError:mod.resultTip];
            }else if (mod.resltState == ResultNetError)
            {
                [MBProgressHUD showError:@"网络错误"];
            }
            else
            {
                [MBProgressHUD showSuccess:@"修改成功"];
            }
            
        });
    });
    
}
#pragma mark 添加门店
-(void)addStoreupdata
{
    [MBProgressHUD showMessage:@"请稍等" toView:self.view];
    __weak typeof(self) baseSelf = self;
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        
        ResulteMod *mod = [dm createshopWith:baseSelf.storeNametf.text];

        dispatch_async(dispatch_get_main_queue(), ^{
            
            [MBProgressHUD hideHUDForView:baseSelf.view animated:YES];
            
            [baseSelf.navigationController popViewControllerAnimated:YES];
            
            if (mod.resltState == ResultFail) {
                [MBProgressHUD showError:mod.resultTip];
            }else if (mod.resltState == ResultNetError)
            {
                [MBProgressHUD showError:@"网络错误"];
            }
            else
            {
                [MBProgressHUD showSuccess:@"添加成功"];
            }
            
        });
    });
}


- (BOOL)textFieldShouldReturn:(UITextField *)textField;{
    
    [self.view endEditing:YES];
    return YES;
}
@end
